#include "TestCase.h"
#include "../core/cbase.h"
#include "../core/CUtil.h"
#include "../Struct.h"
#include "../Config.h"
#include "../Cache.h"
#if defined(__QT__)
	#include "../platform/qt/QTestCase.h"
#endif

P TestCase::s_debugModelPos;
int TestCase::s_genModelID = 0;

void TestCase::run() {
	static bool isRunning = false;// avoid repeated calls
	if (isRunning)
		return;
	isRunning = true;

	if (Config::Released)
		return;

	#if defined(__QT__)
		QTestCase::run();
	#endif

	if (Config::TestCase_SampleDataEnabled)
		CUtil::loop([] { runFixedTestCase(); }, Config::TestCase_SampleDataLoopIntervalMs);// 60Hz
}

void TestCase::debugModelX(float deltaX) {
	s_debugModelPos.x += deltaX;
	LogD << "X = " << s_debugModelPos.x;
}

void TestCase::debugModelZ(float deltaZ) {
	s_debugModelPos.z += deltaZ;
	LogD << "Z = " << s_debugModelPos.z;
}


#define GenID_RANDOM

#ifdef GenID_RANDOM
	#define GenID CMath::randomInt(1, 100)
#elif defined(GenID_FIXED)
	#define GenID ++s_genModelID
#endif

/************************************************************************/
void TestCase::runFixedTestCase() {
	CVector<LineArg> lineArgs;
	lineArgs << LineArg{LineTag::L3, LineEq{{ 6.5, 0.0, -0.0, 0.0 }, -12, -17}, LineStyle::Solid, "white"};
	lineArgs << LineArg{LineTag::L2, LineEq{{ 4.0, 0.0, -0.0, 0.0 }, 1000, -1000}, LineStyle::Dashed, "yellow"};
	lineArgs << LineArg{LineTag::L, LineEq{{ 1.5, 0.0, -0.0, 0.0 }, 1000, -1000}, LineStyle::Dashed, "white"};
	lineArgs << LineArg{LineTag::R, LineEq{{ -1.5, -0.0, 0.0, 0.0 }, 1000, -1000}, LineStyle::Dashed, "yellow"};
	lineArgs << LineArg{LineTag::R2, LineEq{{ -4.0, -0.0, 0.0, 0.0 }, 1000, -1000}, LineStyle::Dashed, "white"};
	lineArgs << LineArg{LineTag::R3, LineEq{{ -6.5, 0.0, -0.0, 0 }, 1000, -1000}, LineStyle::RoadEdge, "yellow"};
	Cache::recvLines(lineArgs);

	CVector<ModelArg> modelArgs;
	s_debugModelPos = P(0, DataFilter::get()->skylineZ() + 30);
	auto debugModel = ModelArg{ 998877, ModelType::Undefined, s_debugModelPos, 0, false, false, "pink" };
	modelArgs << debugModel;

	const int LimitTopZ = DataFilter::get()->skylineZ();
	const int LimitBottomZ = DataFilter::get()->bottomZ();
	static int N = 0;
	static bool SameDir = true;
	float step = N * 0.2;
	auto truckPos = P(-4, 10 - step);
	if (truckPos.z > LimitBottomZ) SameDir = true;
	else if (truckPos.z < LimitTopZ) SameDir = false;
	if (SameDir) ++N;
	else --N;

	ModelArg truck = {100, ModelType::Truck, truckPos, SameDir ? 0.0f : C_PI };
	modelArgs << truck;

	auto carPos = P(CMath::random(3.2, 3.8), CMath::random(-25, 2));
	auto personPos = P(CMath::random(-1, 1), CMath::random(-25, -2));
	modelArgs << ModelArg{101, ModelType::Car, carPos, CMath::random() * 15};
	modelArgs << ModelArg{102, ModelType::Person, personPos };

	modelArgs << ModelArg{ 103, ModelType::Barrier, P(-3.5, 0.0) };
	
	s_genModelID = 0;
	modelArgs << ModelArg{ GenID , ModelType::Car, P(0, -37) };
	modelArgs << ModelArg{ GenID , ModelType::Car, P(-2.8, -37) };
	modelArgs << ModelArg{ GenID , ModelType::Car, P(-6, -37) };
	modelArgs << ModelArg{ GenID , ModelType::Car, P(-8.8, -37) };
	modelArgs << ModelArg{ GenID , ModelType::Car, P(-12, -37) };
	modelArgs << ModelArg{ GenID , ModelType::Car, P(-14.5, -37) };
	modelArgs << ModelArg{ GenID , ModelType::Unknown, P(-12.8, -26.4) };
	modelArgs << ModelArg{ GenID , ModelType::Car, P(-6.8, -5.4) };
	modelArgs << ModelArg{ GenID , ModelType::Car, P(-10.4, -17.5) };
	modelArgs << ModelArg{ GenID , ModelType::Moto, P(-6, 1.6) };
	modelArgs << ModelArg{ GenID , ModelType::Car, P(3.6, 6.6) };
	modelArgs << ModelArg{ GenID , ModelType::Car, P(8, -21.4) };
	modelArgs << ModelArg{ GenID , ModelType::Car, P(8, -3.4) };
	modelArgs << ModelArg{ GenID , ModelType::Truck, P(8, -12.4) };
	modelArgs << ModelArg{ GenID , ModelType::Car, P(8, -33.4) };
	modelArgs << ModelArg{ GenID , ModelType::Car, P(12.8, -33.4) };
	modelArgs << ModelArg{ GenID , ModelType::Cone, P(11.6, -12.4) };
	modelArgs << ModelArg{ GenID , ModelType::Car, P(12.8, -22.4) };
	modelArgs << ModelArg{ GenID , ModelType::Car, P(18.8, -36.4) };
	modelArgs << ModelArg{ GenID , ModelType::Truck, P(-6.8, -22.4) };

	modelArgs << ModelArg{ GenID , ModelType::Cone, P(-4.4, 9.5) };
	modelArgs << ModelArg{ GenID , ModelType::Cone, P(-3.8, 9.5) };
	modelArgs << ModelArg{ GenID , ModelType::Cone, P(-3.2, 9.5) };
	modelArgs << ModelArg{ GenID , ModelType::Cone, P(-2.6, 9.5) };
	modelArgs << ModelArg{ GenID , ModelType::Cone, P(-2.0, 9.5) };

	Cache::recvModels(modelArgs);
}
